import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-08-04
 * Time: 19:50
 */
@WebServlet("/getParameter")
public class GetParameter extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //前端通过url的query string 传递 username 和 password
        String username = req.getParameter("username");
        if(username == null) {
            System.out.println("username 这个 key 在 query string 不存在");
        }
        String password = req.getParameter("password");

        if(password == null) {
            System.out.println("password 这个 key 在 query string 不存在");
        }

        System.out.println("username="+username+",password="+password);
        resp.getWriter().write("ok!");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //前端通过 body 的query string 传递 username 和 password
        req.setCharacterEncoding("utf8");
        String username = req.getParameter("username");
        if(username == null) {
            System.out.println("username 这个 key 在 query string 不存在");
        }
        String password = req.getParameter("password");

        if(password == null) {
            System.out.println("password 这个 key 在 query string 不存在");
        }

        System.out.println("username="+username+",password="+password);
        resp.getWriter().write("ok!");

        //需要转码 urlencode

    }
}
